package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.AddSellerPorductTagsBean;
import com.ymatou.iapi.productmanager.service.AddSellerPorductTagsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.TestDataManager;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*新增营销标签
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AddSellerPorductTags{
	private static AddSellerPorductTagsBean addsellerporducttagsBean;
	private static AddSellerPorductTagsCall addsellerporducttagsCall;
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新增营销标签");
	}

	@Before
	public void caseUp() {
		addsellerporducttagsBean=new AddSellerPorductTagsBean();
		addsellerporducttagsCall=new AddSellerPorductTagsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addsellerporducttagsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 根据条件获取ids
	 * @param sellerid
	 * @param tid
	 * @return
	 */
	public List getIds(Integer sellerid,Integer tid) {
		List<Integer> ids=new ArrayList<Integer>();
		List<Map> list = intergratedProductMapper.selectYmtSellerProductTag(sellerid, tid);
		for (Map map : list) {
			ids.add(Integer.valueOf( map.get("Id").toString()));
		}
		return ids;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddSellerPorductTags_001() {
		Logger.start(true,"新增营销标签成功");
		try {
			Integer sellerId=1;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("200", addsellerporducttagsCall.getString("Code"), "验证code");
			int id=addsellerporducttagsCall.getJsonObject().get("Data").getAsJsonObject().get("tag").getAsJsonObject().get("id").getAsInt();
			List<Map> list = intergratedProductMapper.selectYmtSellerProductTag(addsellerporducttagsBean.getSellerId(), addsellerporducttagsBean.getThirdcategoryId());
			Map m = list.stream().filter(x->{return x.get("Tag").toString().equals(addsellerporducttagsBean.getTags());}).findAny().get();
			Logger.debug(m);
			Logger.verifyNotNull(m, "营销标签查询成功");
			Logger.verifyEquals(addsellerporducttagsBean.getSellerId(), m.get("SellerId"), "验证sellerid");
			Logger.verifyEquals(addsellerporducttagsBean.getTags(), m.get("Tag"), "验证Tag");
			Logger.verifyEquals(addsellerporducttagsBean.getCategoryId(), m.get("CategoryId"), "验证CategoryId");
			Logger.verifyEquals(addsellerporducttagsBean.getThirdcategoryId(), m.get("ThirdcategoryId"), "验证ThirdcategoryId");
			Logger.verifyEquals(0, m.get("Status"), "验证Status");
			//{CategoryId=1001, Status=0, CreateTime=2020-09-09 10:13:23.72, UpdateTime=2020-09-09 10:13:23.72, SellerId=1, Id=28, Tag=Test648, LastUseTime=2020-09-09 10:13:23.72, ThirdcategoryId=1003}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_002() {
		Logger.start(true,"不输入sellerid失败");
		try {
			Integer sellerId=2;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
//			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
//			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("400", addsellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_003() {
		Logger.start(true,"不输入categoryId失败");
		try {
			Integer sellerId=3;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
//			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
//			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("400", addsellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_004() {
		Logger.start(true,"不输入thirdcategoryId失败");
		try {
			Integer sellerId=4;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
//			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
//			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("400", addsellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_005() {
		Logger.start(true,"不输入tags失败");
		try {
			Integer sellerId=5;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
//			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
//			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("400", addsellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_006() {
		Logger.start(true,"买手三级类目营销标签不能超过20个(20210111 等待研发修改)");
		try {
			Integer sellerId=6;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			
			for (int i = 0; i < 19; i++) {
				addsellerporducttagsBean.setSellerId(sellerId);
				addsellerporducttagsBean.setCategoryId(cid);
				addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
				addsellerporducttagsBean.setTags("Test"+i);
				addsellerporducttagsCall.setData(addsellerporducttagsBean);
				addsellerporducttagsCall.callService();			
				Logger.verifyEquals("200", addsellerporducttagsCall.getString("Code"), "验证code");
			}
			addsellerporducttagsBean.setTags("TestEnd");
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("201", addsellerporducttagsCall.getString("Code"), "验证code");
			//买手营销标签不能超过20个
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_007() {
		Logger.start(true,"已存在相同标签失败");
		try {
			Integer sellerId=7;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(3));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("200", addsellerporducttagsCall.getString("Code"), "验证code");
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("201", addsellerporducttagsCall.getString("Code"), "验证code");
			//已存在相同标签
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_008() {
		Logger.start(true,"标签长度超出12个字符失败");
		try {
			Integer sellerId=8;
			Integer cid=1001;
			Integer thirdcategoryid=1003;
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(9));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("400", addsellerporducttagsCall.getString("Code"), "验证code");
			//标签长度不符合
			addsellerporducttagsBean.setTags("Test"+TestDataManager.getRandomNum(8));
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("200", addsellerporducttagsCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_009() {
		Logger.start(true,"买手三级类目营销标签加一级固定标签不能超过20个(20210111 等待研发修改)");
		try {
			//sellerid 0 为一级固定标签 1086 测试标签
			Integer sellerId=9;
			Integer cid=1086;
			Integer thirdcategoryid=1003;
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			
			for (int i = 0; i < 19; i++) {
				addsellerporducttagsBean.setSellerId(sellerId);
				addsellerporducttagsBean.setCategoryId(cid);
				addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
				addsellerporducttagsBean.setTags("Test"+i);
				addsellerporducttagsCall.setData(addsellerporducttagsBean);
				addsellerporducttagsCall.callService();			
				Logger.verifyEquals("200", addsellerporducttagsCall.getString("Code"), "验证code");

				System.out.println("i:" +i);
			}
			addsellerporducttagsBean.setTags("TestEnd");
			addsellerporducttagsCall.callService();			
			Logger.verifyEquals("201", addsellerporducttagsCall.getString("Code"), "验证code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_AddSellerPorductTags_010() {
		Logger.start(true,"已存在和一级固定类目相同标签失败");
		try {
			//sellerid 0 为一级固定标签 1086 测试标签
			Integer sellerId=10;
			Integer cid=1086;
			Integer thirdcategoryid=1003;
			ProductManagerCallServiceV3.DeleteAllSellerPorductTags(sellerId, thirdcategoryid);
			addsellerporducttagsBean.setSellerId(sellerId);
			addsellerporducttagsBean.setCategoryId(cid);
			addsellerporducttagsBean.setThirdcategoryId(thirdcategoryid);
			addsellerporducttagsBean.setTags("测试标签");
			addsellerporducttagsCall.setData(addsellerporducttagsBean);
			addsellerporducttagsCall.callService();
			addsellerporducttagsCall.callService();
			Logger.verifyEquals("201", addsellerporducttagsCall.getString("Code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}